/************************************************************************************/
/*  Program name CAPAPIMP_AP_BANK_AC_USE.sql                                        */
/*  Creater:  Nip, Karen (PCCW - UniHub)                                            */
/*************   History   *************************                                */
/* 21 DEC, 2005   Nip, Karen            First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_TOLERANCE';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_TOLERANCES;

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO AP_TOLERANCES_ALL
			VALUES
			(
			C1.PRICE_TOLERANCE,
			C1.QUANTITY_TOLERANCE,
			C1.LAST_UPDATE_DATE,
			C1.LAST_UPDATED_BY,		--LAST_UPDATED_BY,
			C1.QTY_RECEIVED_TOLERANCE,
			C1.TAX_TOLERANCE,
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.MAX_QTY_ORD_TOLERANCE,
			C1.MAX_QTY_REC_TOLERANCE,
			C1.SHIP_AMT_TOLERANCE,
			C1.RATE_AMT_TOLERANCE,
			C1.TOTAL_AMT_TOLERANCE,
			C1.TAX_TOL_AMT_RANGE,
			CAPBRIMP_MAPPING.GetOrgMapping(C1.OU_NAME)
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
